<template>
  <div class="edit-container">
    <el-card class="edit-card">
      <template #header>
        <div class="card-header">
          <div class="title">
            <el-icon><Menu /></el-icon>
            {{ isEdit ? '编辑食品记录信息' : '新增膳食记录' }}
          </div>
          <div class="header-actions">
            <el-button @click="goBack">取消</el-button>
            <el-button type="primary" @click="submitForm">保存</el-button>
          </div>
        </div>
      </template>

      <div class="form-content">
        <div class="section-title">
          <el-icon><InfoFilled /></el-icon>
          <h3>食品信息</h3>
        </div>
        
        <el-form 
          ref="formRef" 
          :model="formData" 
          :rules="rules" 
          label-width="120px"
          label-position="right"
        >
          <el-row :gutter="30">
            <el-col :span="12">
              <el-form-item label="老人ID" prop="elderlyId">
                <el-input v-model="formData.elderlyId" placeholder="请输入老人ID">
                  <template #prefix>
                    <el-icon><User /></el-icon>
                  </template>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="膳食模板" prop="mealId">
  <el-select 
    v-model="formData.mealId" 
    placeholder="请选择膳食模板"
    clearable
    class="meal-select"
  >
    <el-icon slot="prefix"><Notebook /></el-icon>
    <el-option
      v-for="item in mealOptions"
      :key="item.value"
      :label="item.label"
      :value="item.value"
    >
      <el-tag
        :type="{
          '0': '',
          '1': 'info',
          '2': 'success',
          '3': 'primary',
          '4': 'warning',
          '5': 'danger'
        }[item.value]"
        class="meal-tag"
      >
        {{ item.label }}
      </el-tag>
    </el-option>
  </el-select>
</el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="30">
            <el-col :span="12">
              <el-form-item label="膳食日期" prop="date">
                <el-date-picker
                  v-model="formData.date"
                  type="date"
                  placeholder="选择日期"
                  value-format="YYYY-MM-DD"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="用餐时间" prop="mealTime">
                <el-time-picker
                  v-model="formData.mealTime"
                  placeholder="选择时间"
                  value-format="HH:mm:ss"
                />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="30">
            <el-col :span="12">
              <el-form-item label="用餐状态" prop="status">
                <el-select 
                  v-model="formData.status" 
                  placeholder="请选择状态" 
                  clearable
                >
                  <el-option
                    v-for="item in statusOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="记录人ID" prop="caregiverId">
                <el-input v-model="formData.caregiverId" placeholder="请输入记录人ID">
                  <template #prefix>
                    <el-icon><Avatar /></el-icon>
                  </template>
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-form-item label="反馈/备注" prop="feedback">
            <el-input 
              v-model="formData.feedback" 
              type="textarea"
              :rows="4"
              placeholder="请输入反馈信息"
            />
          </el-form-item>
          
          <div class="form-footer">
            <el-button @click="goBack">取消</el-button>
            <el-button type="primary" @click="submitForm">保存记录</el-button>
          </div>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { findById, save } from '@/api/food'
import { ElMessage } from 'element-plus'
import {
  Menu,
  InfoFilled,
  User,
  Notebook,
  Avatar
} from '@element-plus/icons-vue'

const route = useRoute()
const router = useRouter()
const formRef = ref()

const formData = reactive({
  elderlyId: '',
  mealId: '',
  date: '',
  mealTime: '',
  status: '',
  feedback: '',
  caregiverId: ''
})
const mealOptions = [
  { value: 0, label: '常规健康' },
  { value: 1, label: '较健康' },
  { value: 2, label: '健康' },
  { value: 3, label: '非常健康' },
  { value: 4, label: '医疗级健康' },
  { value: 5, label: '极致健康' }
]
const statusOptions = [
  { value: 0, label: '未食用' },
  { value: 1, label: '已食用' },
  { value: 2, label: '部分食用' },
  { value: 3, label: '未提供' },
  { value: 4, label: '异常' }
]

const isEdit = computed(() => route.query.id !== undefined)

const rules = {
  elderlyId: [{ required: true, message: '请输入老人ID', trigger: 'blur' }],
  mealId: [{ required: true, message: '请选择膳食模板', trigger: 'blur' }],
  date: [{ required: true, message: '请选择日期', trigger: 'blur' }],
  mealTime: [{ required: true, message: '请选择时间', trigger: 'blur' }],
  status: [{ required: true, message: '请选择用餐状态', trigger: 'change' }],
  caregiverId: [{ required: true, message: '请输入记录人ID', trigger: 'blur' }]
}

const loadEmployeeData = async (id) => {
  try {
    const res = await findById(id)
    Object.assign(formData, res.data)
  } catch (error) {
    console.error('加载食品数据失败:', error)
    ElMessage.error('加载食品数据失败')
  }
}

const submitForm = async () => {
  try {
    await formRef.value.validate()
    
    if (isEdit.value) {
      await save(formData)
      ElMessage.success('食品信息更新成功')
    } else {
      await save(formData)
      ElMessage.success('食品记录添加成功')
    }
    
    goBack()
  } catch (error) {
    console.error('保存失败:', error)
    if (!error.response) {
      ElMessage.error('表单验证失败，请检查输入')
    }
  }
}

const goBack = () => {
  router.go(-1)
}

onMounted(() => {
  if (isEdit.value) {
    loadEmployeeData(route.query.id)
  }
})
</script>

<style scoped>
.edit-container {
  padding: 20px;
  background: linear-gradient(135deg, #f5f7fa 0%, #e4edf9 100%);
  min-height: 100vh;
}

.edit-card {
  margin: 0 auto;
  max-width: 1000px;
  border-radius: 20px;
  overflow: hidden;
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.12);
  border: none;
}

.card-header {
  background: linear-gradient(120deg, #4a6bdf 0%, #3a56c4 100%);
  padding: 20px 30px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-header .title {
  color: white;
  font-size: 22px;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 12px;
}

.header-actions {
  display: flex;
  gap: 12px;
}

.header-actions .el-button {
  height: 40px;
  padding: 0 24px;
  border-radius: 10px;
  font-weight: 500;
}

.form-content {
  padding: 30px;
  background: white;
}

.section-title {
  display: flex;
  align-items: center;
  margin-bottom: 25px;
  padding-bottom: 10px;
  border-bottom: 2px solid #f0f4f8;
}

.section-title h3 {
  color: #4a6bdf;
  font-size: 18px;
  margin-left: 10px;
}

.el-form-item {
  margin-bottom: 28px;
}

.el-form-item :deep(.el-form-item__label) {
  font-weight: 500;
  color: #5a6d88;
}

.el-input, .el-date-editor, .el-time-picker, .el-select {
  border-radius: 12px;
  overflow: hidden;
}

.el-input :deep(.el-input__inner),
.el-date-editor :deep(.el-input__inner),
.el-time-picker :deep(.el-input__inner),
.el-select :deep(.el-input__inner) {
  height: 48px;
  padding-left: 15px;
  font-size: 16px;
}

.el-date-editor, .el-time-picker, .el-select {
  width: 100%;
}

.el-row {
  margin-bottom: 10px;
}

.form-footer {
  margin-top: 30px;
  display: flex;
  justify-content: center;
  gap: 20px;
}

.form-footer .el-button {
  width: 160px;
  height: 50px;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 500;
  transition: all 0.3s ease;
}

.form-footer .el-button:hover {
  transform: translateY(-3px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.el-button--primary {
  background: linear-gradient(120deg, #4a6bdf 0%, #3a56c4 100%);
  border: none;
}
.meal-select {
  width: 100%;
}

.meal-select :deep(.el-input__inner) {
  padding-left: 40px;
}

.meal-tag {
  width: 100%;
  text-align: center;
  border: none;
  padding: 8px 12px;
}

@media (max-width: 768px) {
  .el-col {
    width: 100%;
    margin-bottom: 15px;
  }
  
  .card-header {
    flex-direction: column;
    gap: 15px;
    align-items: flex-start;
  }
  
  .header-actions {
    width: 100%;
    justify-content: flex-end;
  }
}
</style>